<?php

class InstrumentController extends Controller
{
    public $layout = '//layouts/topColumn';

    public $menuList;

    public $menuitemName;

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
				'minLength'=>4,
				'maxLength'=>4,
                'testLimit'=>1,
            ),
		);
    }

    /**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('captcha','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('index','ajaxdatetable','reserveinfo','deletereserve'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
     
	public function actionIndex()
    {
        if(isset($_POST['yoo']))
        {
            $iid = $_POST['yoo'];
            $empty = true;
            foreach($_POST['checkbox'] as $key=>$value)
            {
                if($value == 1)
                {
                    $empty =false;
                    //存入预约表
                    $model = new InstrumentReserve;
                    $model->instrument_id = $iid;
                    $model->elem_id = -1;
                    $model->user_id = yii::app()->user->id;
                    $model->start_date = $key;
                    if(!$model->save())
                    {
                        throw new CHttpException('分配仪器失败, 请与管理员联系。');
                    }
                }
            }
            if($empty)
            {
                echo "<script>alert(\"请至少选择一个时间段进行预定。(Please select at least one time to reserve)\")</script>";
                echo "<script>history.go(-1);</script>";
            }
            $this->redirect(array('reserveInfo'));
        }
        if(isset($_GET['search']))
        {
            $key = $_GET['search'];
            $model = Instrument::model()->findAll("title like '%{$key}%'");
            $this->render('index',array(
                'search'=>true,
                'model'=>$model,
                'key'=>$key,
            ));
            return;
        }
        $dataProvider=new CActiveDataProvider('Instrument',
		array(
			'criteria'=>array(
				'order'=>'instrument_id ASC',
			),
	));
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }
    public function actionView($id)
    {
         $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }
    public function actionAjaxDateTable()
    {
        if(isset($_POST['iid']))
        {
           $iid = $_POST['iid'];
            $dateTable = InstrumentDate::GenerateReserveDateTableByID($iid);
            $model = Instrument::model()->findByPk($iid);
            $this->renderPartial('_statisticsdatetable', array('model'=>$model,'dateTable'=>$dateTable));
        }
    }

    public function actionReserveInfo()
    {
        $model=new InstrumentReserve('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['InstrumentReserve']))
        {
            $model->attributes=$_GET['InstrumentReserve'];
        }

        $this->render('reserveinfo',array(
            'model'=>$model,
        ));
    }
	public function loadModel($id)
	{
		$model=Instrument::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

/**
     * =======================================================
     *               用户预约管理
     * =======================================================
     */
    public function actionDeleteReserve($id)
    {
        $model = $this->loadReserveModel($id);
        $iid = $model->getInstrumentID();
        $model->delete();
        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('reserveinfo','id'=>$iid));
    }
    public function loadReserveModel($id)
    {
        $model=InstrumentReserve::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}
